<?php
/**
 * ${NAME}
 * @Author Vee7
 * @Date 2021/12/23
 * @Time 13:37
 * @Description Vee7
 */

declare (strict_types=1);

namespace app\api\base;

use app\api\common\Output;
use think\Request;
use think\App;

/**
 * api基础类
 */
abstract class ApiBase
{
    use Output;

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 不需要登录的方法
     */
    protected $noAuth = [];

    /***
     * 需要签名校验的方法，优先级小于noAuth
     */
    protected $needSign = [];

    /**
     * BaseException constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->init();

    }

    /**
     * @return bool
     */
    public function init(){
        // 检测方法是否需要Token认证
        if(in_array($this->request->action(),$this->noAuth)){
            //判断是否需要进行Sign签名认证
            if(!in_array($this->request->action(),$this->needSign)){
                return true;
            }else{
                array_push($this->middleware,\app\middleware\api\SignAuth::class);
            }
        }else{//控制器中间件
            $this->middleware = [
                //授权认证中间件
                \app\api\middleware\TokenAuth::class,
                //接口日志记录中间件
                \app\middleware\api\ApiLog::class
            ];
            //判断是否需要进行签名认证
            if(in_array($this->request->action(),$this->needSign)){
                array_push($this->middleware,\app\middleware\api\SignAuth::class);
            }
        }

    }


}
